using Commons.Models.OperationsContext;
using Commons.Models.Stats;
using Commons.Services.Stats;

namespace Commons.Services.Functions
{
	public class UserStatVariableEvaluator : IVariableEvaluator
	{
		protected const string Prefix = "S:";

		public IUserStatsService UserStatsService { protected get; set; }

		public double? Eval(string variable, IOperationContext context)
		{
			if (variable.StartsWith(Prefix))
			{
				UserStats stats = UserStatsService.GetStats();

				string statName = variable.Substring(Prefix.Length).ToLower();
				if (statName.EndsWith("_level"))
				{
					StatUnit unit = StatUnit.GetByName(statName.Split('_')[0]);

					return stats.GetStat<LeveledStat>(unit).GetLevel().Num;
				}
				else
				{
					StatUnit unit = StatUnit.GetByName(statName);

					return stats.GetStat<IStat>(unit).Value;
				}
			}

			return null;
		}
	}
}

